# -*- coding: utf-8 -*-
import scrapy
import re
from scrapy.http import Request
from pictures.items import picturespiderItem
import logging
logger = logging.getLogger(__name__)

class picturespider(scrapy.Spider):

    name = "picturespider"
    cover = "picturespider"
    title = "picturespider"
    allowed_domains = ["m.yituyu.com"]
    start_urls = ["https://m.yituyu.com/"]

    def start_requests(self):
        yield Request(url=f"https://m.yituyu.com")

    def parse(self, response):
        #   解析标签

        label_list = response.xpath("//div[@class='tags']/a")

        for label in label_list:
            valueLabel = label.xpath("@href").extract_first()
            name = label.xpath("./text()").extract_first()

            for page in range(1, 11):
                # /tag/180/p1/
                yield Request(url=f"https://m.yituyu.com{valueLabel}p{page}", callback=self.parse_info,meta={"title":name})
        # yield Request(url=f"https://m.yituyu.com/tag/180/p1/", callback=self.parse_info)

    def parse_info(self, response):
        title=response.meta["title"]
        li_list = response.xpath("//div[@class='box']/ul/li")
        for li in li_list:
             data_url = li.xpath("div[@class='img-box']/a/@href").extract_first()
             cover = f"https://img.yituyu.com{data_url}cover.jpg"
            #  title=li.xpath("p[2]/a/text()").extract_first()
             yield Request(url=f"https://m.yituyu.com{data_url}", callback=self.parse_info2,meta={"cover":cover,"title":title})

    def parse_info2(self, response):
        item = picturespiderItem()
        imgs = response.xpath("//div[@class='pic']//img/@src").extract()
        imgNames = response.xpath("//div[@class='pic']//img/@alt").extract()
        # imgNames2 = imgNames.xpath("normalize-space(substring-after(substring-before(., '》'), '《'))")
       
        item['images']=imgs
        item['type']="pic"
        item['image_names']=imgNames
        item['cover']= response.meta["cover"]
        item['title']=response.meta["title"]
       

        yield item
     
       